package com.apes.framework.plugin.fsm;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.fsm.core.ApesDotVisitor;
import com.apes.framework.plugin.fsm.core.FsmConfig;
import com.apes.framework.plugin.fsm.core.FsmManager;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.squirrelframework.foundation.component.SquirrelProvider;
import org.squirrelframework.foundation.fsm.DotVisitor;
import org.squirrelframework.foundation.fsm.StateMachine;

/**
 * Created by lishikun on 2020/8/29 17:22
 */
@Service("fsmFlowService")
public class FsmFlowService extends DomainService {

    @Autowired
    private FsmFlowRepository fsmFlowRepository;

    /***
     * 功能：保存
     * @param request
     * @return
     */
    public FsmFlow sava(SimpleRequest request) {
        FsmFlow fsmFlow = request.getO(FsmFlow.class);
        fsmFlow = fsmFlowRepository.saveAndFlush(fsmFlow);
        return fsmFlow;
    }

    /**
     * 功能：修改
     *
     * @param request
     * @return
     */
    public FsmFlow update(SimpleRequest request) {
        String id = request.get("id");
        FsmFlow fsmFlow = request.getO(FsmFlow.class);
        FsmFlow tempFsmFlow = fsmFlowRepository.findOne(id);
        if (tempFsmFlow == null) throw new RuntimeException("状态" + id + "】在系统中不存在。");
        if (tempFsmFlow.getVersion() != fsmFlow.getVersion())
            throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改
        return fsmFlowRepository.saveAndFlush(fsmFlow);
    }

    /***
     * 功能：删除
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        String id = request.get("id");
        FsmFlow fsmFlow = request.getO(FsmFlow.class);
        FsmFlow tempFsmState = fsmFlowRepository.findOne(id);
        if (tempFsmState == null) throw new RuntimeException("状态" + id + "】在系统中不存在。");
        if (tempFsmState.getVersion() != fsmFlow.getVersion()) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        fsmFlowRepository.delete(fsmFlow);
        return "删除成功";
    }

    @OutboxSubscriber(name = "状态流：增加")
    public void subscribeSaveEvent(FsmFlow fsmFlow) {
        SpringManager.getBean(StringRedisTemplate.class).convertAndSend(FsmConfig.FSM_SETTING_TOPIC, fsmFlow.getId());
    }

    @OutboxSubscriber(name = "状态流：删除", eventType = "D")
    public void subscribeDeleteEvent(FsmFlow fsmFlow) {
        SpringManager.getBean(StringRedisTemplate.class).convertAndSend(FsmConfig.FSM_SETTING_TOPIC, fsmFlow.getId());
    }

    /****/
    public String flowChart(SimpleRequest request) {
        FsmFlow fsmFlow = fsmFlowRepository.findOne((String) request.get("id"));
        if (fsmFlow == null) return null;
        StateMachine stateMachine = SpringManager.getBean(FsmManager.class).createStateMachine(fsmFlow);
        if (stateMachine == null) {
            return null;
        }
        DotVisitor visitor = SquirrelProvider.getInstance().newInstance(ApesDotVisitor.class);
        stateMachine.accept(visitor);
        return visitor.toString();
    }
}
